﻿using Cherry.Agile.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Cherry.Agile.UI.Framework.Security
{
    internal class AuthrizationCacheStorage : DefatultCacheStorage, IDisposable
    {
        const string cacheKey = "__authrization";
        public AuthrizationCacheStorage()
            : base()
        {

        }

        public override object this[string key]
        {
            get
            {
                if (HttpContext.Current == null
                    || HttpContext.Current.Session == null)
                    return null;
                //    && HttpContext.Current.Session[cacheKey] != null)
                return HttpContext.Current.Session[cacheKey];
                //var obj = base[key];

                //if (HttpContext.Current != null 
                //    && HttpContext.Current.Session != null)
                //    HttpContext.Current.Session[cacheKey] = obj;
                //return obj;                
            }
            set
            {
                base[key] = value;
                if (HttpContext.Current != null && HttpContext.Current.Session != null)
                    HttpContext.Current.Session[cacheKey] = value;
            }
        }


        public void Dispose()
        {

        }
    }


    //class CacheItemExpiration: IDisposable
    //{
    //    TimeSpan? _sildingTime = TimeSpan.FromMinutes(25);
    //    public CacheItemExpiration()
    //    {
    //        AuthorizationSyncListener.Changed += new EventHandler<AuthorizationChangedEventArgs>(AuthorizationSyncListener_Changed);
    //    }

    //    void AuthorizationSyncListener_Changed(object sender, AuthorizationChangedEventArgs e)
    //    {
    //        if ((e.Type & _type) != _type)
    //            return;
    //        if ((e.Type & AuthorizationDataType.User) == AuthorizationDataType.User)
    //        {
    //            if (e.Args[0] == null)
    //                throw new ArgumentNullException("当用户信息改变是必须设置参数的账户信息");
    //            var account = e.Args[0].ToString();
    //            expired = account.Equals(value.ToString(), StringComparison.OrdinalIgnoreCase);
    //        }
    //        if ((e.Type & AuthorizationDataType.Other) == AuthorizationDataType.Other)
    //        {
    //            if (e.Args[0] == null)
    //                throw new ArgumentNullException("当用户其他相关信息发生改变是，必须制定具体改变数据的类型");
    //            var val = e.Args[0].ToString();
    //            expired = val.Equals(value.ToString(), StringComparison.OrdinalIgnoreCase);
    //        }
    //    }

    //    AuthorizationDataType _type = AuthorizationDataType.Other;
    //    bool expired;
    //    object value;
    //    public bool HasExpired()
    //    {
    //        return expired || _sildingTime.HasExpired();
    //    }

    //    public void Initialize(CacheItem owningCacheItem)
    //    {
    //        var type = owningCacheItem.Key.Split('@')[1];
    //        string[] names = Enum.GetNames(typeof(AuthorizationDataType));
    //        if (names.Contains(type))
    //            this._type = (AuthorizationDataType)Enum.Parse(typeof(AuthorizationDataType), type);
    //        else
    //            this._type = AuthorizationDataType.Other;

    //        if (_type == AuthorizationDataType.User)
    //        {
    //            var user = owningCacheItem.Value as IIdentity;
    //            if (user == null || string.IsNullOrEmpty(user.Name))
    //                throw new ArgumentNullException("缓存项的类型必须为User同时不能为空");
    //            value = user.Name;
    //        }
    //        else if (_type == AuthorizationDataType.Other)
    //        {
    //            value = type;
    //        }

    //    }

    //    public void Notify()
    //    {
    //        _sildingTime.Notify();
    //    }

    //    public void Dispose()
    //    {
    //        AuthorizationSyncListener.Changed -= new EventHandler<AuthorizationChangedEventArgs>(AuthorizationSyncListener_Changed);
    //    }
    //}
}
